package net.xmat.webtest.utils;

import net.xmat.webtest.beans.Book;
import net.xmat.webtest.beans.Author;
import net.xmat.webtest.beans.Publisher;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

public class DatabaseLoader {
	public void loadDatabase() {
		SessionFactory sessionFactory = HibernateUtils.getSessionFactory();
		Author author1 = Author.create("Matei", 21);
		Publisher p = Publisher.create("Teora");
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		
		session.saveOrUpdate(author1);
		session.saveOrUpdate(p);
		
		Book book = new Book("Test book", "22234-4321-5678-1231", 10f, 4.5f);
		book.setAuthor(author1);
		book.setPublisher(p);
		
		session.saveOrUpdate(book);

		book = new Book("Test book 1", "22234-4321-5678-1212", 10f, 4.5f);
		book.setAuthor(author1);
		book.setPublisher(p);

		session.saveOrUpdate(book);

		book = new Book("Test book 2", "22234-4321-5678-1210", 13f, 4.f);
		book.setAuthor(author1);
		book.setPublisher(p);

		session.saveOrUpdate(book);
		
		session.getTransaction().commit();
		session.close();
		
	}
	
	
}
